using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class BannerManager
	{
		private const string TAG = "BannerManager";
		private static BannerInfo currentBanner;
		private EventWatcher bannerImgWatcher;
		public BannerManager()
		{
			this.bannerImgWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.OnGetBannerImg));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GET_PSM_IMG_SUCCESS, this.bannerImgWatcher);
		}
		public static string GetBannerUrl()
		{
			if (BannerManager.currentBanner == null)
			{
				return null;
			}
			if (string.IsNullOrEmpty(BannerManager.currentBanner.string_1))
			{
				return null;
			}
			return BannerManager.currentBanner.string_1;
		}
		public static string GetBannerTipId()
		{
			if (BannerManager.currentBanner == null)
			{
				return "";
			}
			if (string.IsNullOrEmpty(BannerManager.currentBanner.string_0))
			{
				return "";
			}
			return BannerManager.currentBanner.string_0;
		}
		public static bool isCanClose()
		{
			return BannerManager.currentBanner == null || BannerManager.currentBanner.enableclose;
		}
		private void OnGetBannerImg(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			string text = evtArgs.mObject as string;
			if (text == null)
			{
				return;
			}
			if (BannerManager.currentBanner == null)
			{
				return;
			}
			if (BannerManager.currentBanner.org_hd_img != text)
			{
				return;
			}
			BannerManager.currentBanner.isImgDownloaded = true;
			BannerManager.SaveBanner();
			EventCenter.postEvent(EventConst.ON_UI_NEED_SHOW_BANNER, null, null);
		}
		public static void CloseBanner()
		{
			if (BannerManager.currentBanner == null)
			{
				return;
			}
			BannerManager.currentBanner.isClosed = true;
			BannerManager.SaveBanner();
		}
		public static string GetBannerPath()
		{
			return StorageIO.getBannerPath() + "/banner.jpg";
		}
		public static void OnGetBannerMsg(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (!chatMsg_0.IsPushMsg())
			{
				return;
			}
			BannerInfo bannerInfo = BannerManager.ParseBanner(chatMsg_0.strMsg);
			if (bannerInfo == null)
			{
				Log.smethod_1("BannerManager", "OnGetBannerMsg:newBanner == null," + chatMsg_0.strMsg);
				return;
			}
			BannerManager.currentBanner = bannerInfo;
			BannerManager.SaveBanner();
			BannerManager.GetBannerImg(BannerManager.currentBanner);
		}
		public static bool IsBannerShouldShow()
		{
			if (BannerManager.currentBanner == null)
			{
				return false;
			}
			if (BannerManager.currentBanner.isClosed)
			{
				return false;
			}
			if (BannerManager.currentBanner.start >= BannerManager.currentBanner.dateTime_0)
			{
				return false;
			}
			if (BannerManager.currentBanner.start > DateTime.get_Now())
			{
				return false;
			}
			if (BannerManager.currentBanner.dateTime_0 < DateTime.get_Now())
			{
				return false;
			}
			if (!BannerManager.currentBanner.isImgDownloaded)
			{
				BannerManager.GetBannerImg(BannerManager.currentBanner);
				return false;
			}
			return true;
		}
		public static void LoadBanner()
		{
			BannerManager.currentBanner = ConfigMgr.read<BannerInfo>();
		}
		private static void SaveBanner()
		{
			if (BannerManager.currentBanner == null)
			{
				return;
			}
			ConfigMgr.write<BannerInfo>(BannerManager.currentBanner);
		}
		public static BannerInfo ParseBanner(string strContent)
		{
			if (string.IsNullOrEmpty(strContent))
			{
				Log.smethod_5("BannerManager", "ParseBanner, input invalid para");
				return null;
			}
			try
			{
				XDocument xDocument = XDocument.Parse(strContent);
				XElement xElement = xDocument.Element("tips").Element("tip");
				XElement xElement2 = xDocument.Element("tips").Element("tip").Element("images");
				BannerInfo bannerInfo = new BannerInfo();
				XAttribute xAttribute = xElement.Attribute("id");
				if (xAttribute != null)
				{
					bannerInfo.string_0 = xAttribute.get_Value();
				}
				xAttribute = xElement.Attribute("enableclose");
				if (xAttribute != null)
				{
					bannerInfo.enableclose = bool.Parse(xAttribute.get_Value());
				}
				xAttribute = xElement.Attribute("transparentclose");
				if (xAttribute != null)
				{
					bannerInfo.transparentclose = bool.Parse(xAttribute.get_Value());
				}
				using (IEnumerator<XElement> enumerator = xElement2.Elements().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						XElement current = enumerator.get_Current();
						if (current.get_Name() == "image")
						{
							if (current.Attribute("type").get_Value() == "hd")
							{
								bannerInfo.org_hd_img = current.get_Value();
							}
							else
							{
								if (current.Attribute("type").get_Value() == "sd")
								{
									bannerInfo.org_sd_img = current.get_Value();
								}
							}
						}
					}
				}
				XElement xElement3 = xDocument.Element("tips").Element("tip").Element("url");
				bannerInfo.string_1 = xElement3.get_Value();
				XElement xElement4 = xDocument.Element("tips").Element("tip").Element("time").Element("continue");
				if (xElement4 != null)
				{
					bannerInfo.continueDays = int.Parse(xElement4.get_Value());
				}
				DateTimeFormatInfo dateTimeFormat = new CultureInfo("zh-CN").get_DateTimeFormat();
				XElement xElement5 = xDocument.Element("tips").Element("tip").Element("time").Element("start");
				if (xElement5 != null)
				{
					bannerInfo.start = DateTime.ParseExact(xElement5.get_Value(), "yyyy-MM-dd-HH", dateTimeFormat);
				}
				XElement xElement6 = xDocument.Element("tips").Element("tip").Element("time").Element("end");
				if (xElement6 != null)
				{
					bannerInfo.dateTime_0 = DateTime.ParseExact(xElement6.get_Value(), "yyyy-MM-dd-HH", dateTimeFormat);
				}
				if (bannerInfo.continueDays > 0)
				{
					bannerInfo.start = DateTime.get_Now();
					bannerInfo.dateTime_0 = DateTime.get_Now().AddDays((double)bannerInfo.continueDays);
				}
				return bannerInfo;
			}
			catch (Exception ex)
			{
				Log.smethod_5("BannerManager", strContent + "," + ex.get_Message());
			}
			return null;
		}
		public static void GetBannerImg(BannerInfo info)
		{
			if (info == null)
			{
				return;
			}
			if (info.org_hd_img == null)
			{
				return;
			}
			ServiceCenter.sceneGetPSMImg.doScene(info.org_hd_img);
		}
		public static void test()
		{
			BannerManager.IsBannerShouldShow();
		}
	}
}
